/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proyectocatalina;

import CapadeDatos.ConexionBD;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JProgressBar;

public class backup extends Thread  implements Runnable{
    String host;
    String usuario;
    String password;
    String rutabck;
    JProgressBar jb;
    public backup(String host1,String usuario1,String password1,String rutabck1,JProgressBar jb2){
        host = host1;
        usuario = usuario1;
        password = password1;
        rutabck = rutabck1;
        jb = jb2;
    }
        int i = 0;
    @Override
public void run(){
        jb.setValue(i);
        i++;
        String bd = ConexionBD.bd;
        String command = "mysqldump -h "+host+"  -u "+usuario+" -p "+password+"  -r"+rutabck+" "+bd+" ";
     Process child = null;
        try {
            child = Runtime.getRuntime().exec(command);
        } catch (IOException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    InputStream input = child.getInputStream();
    FileOutputStream output = null;
        try {
            output = new FileOutputStream(rutabck);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            transfer(input, output);
        } catch (Exception ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
}
public void transfer(InputStream input, OutputStream output) throws Exception {
  byte[] buf = new byte[1024];
  int len;
  while ((len = input.read(buf)) > 0) {
    output.write(buf, 0, len);
  }
  input.close();
  output.close();
}
}

